package com.bkbw.live.common.notifycenter;

import java.util.HashMap;
import java.util.LinkedList;


public class GameNotificationCenter {
	public enum NotifyEvent 
	{
		One,
		Two
	}
	
	protected static GameNotificationCenter instance = new GameNotificationCenter();
	
	public static GameNotificationCenter getInstance() 
	{
		return instance;
	}
	
	protected HashMap<GameNotifyEvent, LinkedList<GameNotifyHandler>> eventTable = new HashMap<GameNotifyEvent, LinkedList<GameNotifyHandler>>();

	
	public void postNotification(GameNotifyEvent event, Object sender, Object param)
	{
		if (eventTable.containsKey(event)) {
			for (GameNotifyHandler handler : eventTable.get(event)) {
				handler.handle(sender, param);
			}
		}
	}
	
	public void registerEventHandler(GameNotifyEvent event, GameNotifyHandler handler)
	{
		//System.out.println("register handler " + handler);
		if (eventTable.containsKey(event)) {
			LinkedList<GameNotifyHandler> li = eventTable.get(event);
			if (!li.contains(handler)) {
				li.add(handler);
			}
		}
		else {
			LinkedList<GameNotifyHandler> li = new LinkedList<GameNotifyHandler>();
			li.add(handler);
			eventTable.put(event, li);
		}
	}
	
	public void removeEventHandler(GameNotifyEvent event, GameNotifyHandler handler)
	{
		if (eventTable.containsKey(event)){
			LinkedList<GameNotifyHandler> li = eventTable.get(event);
			//System.out.println("remove handler " + handler);
			if (li.remove(handler)) {
				//System.out.println("handler removed succ");
			}
			else {
				//System.out.println("handler removed fail");
			}
		}
	}	
	
}
